from flask import jsonify
from . import web
from app.libs.query import add_to_index, query_index
from app.models.user import User
from app.libs.query import es


@web.route('/add/index')
def add_index():
    for user in User.query.all():
        add_to_index('user', user)
    return 'success'


@web.route('/query/all')
def query_all():
    res = es.search(index="user", body={'query': {'match_all': {}}})
    return jsonify(res)


@web.route('/query/single/<q>')
def query_single(q):
    ret = query_index('user', q)
    return jsonify(ret)